package ic.symbols;

import ic.ast.Visitor;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Matan
 * Date: 12/10/13
 * Time: 11:11 AM
 */
public abstract class SymbolTable {
    protected SymbolTable parent;
    protected String name;
    protected Map<String, SymbolTable> childrenSymbolTables = new LinkedHashMap<String, SymbolTable>();
    protected int blockDepth = 0;

    public int getBlockDepth() {
        return blockDepth;
    }

    public SymbolTable(SymbolTable parent) {
        this(parent, "");
    }

    public SymbolTable(SymbolTable parent, String name) {
        this.parent = parent;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    abstract public Symbol lookup(String name);

    abstract public SymbolTable lookupTable(String name);

    abstract public Symbol staticLookup(String name);

    public SymbolTable getParent() {
        return parent;
    }

    public void setParent(SymbolTable parent) {
        this.parent = parent;
    }

    public void insertChildSymbolTable(SymbolTable childTable) {
        childrenSymbolTables.put(childTable.getName(), childTable);
    }

    public List<SymbolTable> getSymbolTables() {
        return new LinkedList<SymbolTable>(childrenSymbolTables.values());
    }

    public SymbolTable getSymbolTable(String tableName) {
        return childrenSymbolTables.get(tableName);
    }

    public Object accept(Visitor visitor) throws Exception {
        return visitor.visit(this);
    }

    public String toString() {
        return name;
    }

    public SymbolTable removeChildTable(String tableName) {
        if (childrenSymbolTables.containsKey(tableName)) {
            return childrenSymbolTables.remove(tableName);
        } else return null;
    }

    public ClassSymbolTable getEnclosingClassTable() {
        return parent.getEnclosingClassTable();
    }

    public abstract String getCurrentMethodName();
}

